import { resolve } from 'path';
import { defHttp } from '/@/common/util/axios';
enum Api {
  orgType = '/base/suitUnit/getStType', //查询考核单位类型
  parentOrg = '/base/suitUnit/upSunitUnit', //查询上级单位
  pointsDefault = '/base/suitUnit/getRaitonType',
  orgGroupList = '/base/stType/findList', //
  update = '/base/suitUnit/update',
  createNewUnit = '/base/suitUnit/create',
  deleteOne = '/base/baseSuitunitOrg/deleteSuitUnit',
  // deptList = '/system/dept/getListById',
  // deptList = '/base/hubKm/findDeptAllTree',
  deptList = '/base/km/dept/findAllTree',
  parentDept = '/base/km/dept/findDept',
  // getUnitTree = '/base/baseSuitunitOrg/findTree', //上级部门和绩效单位树
  getUnitTree = '/base/hubKm/findSuitUnitOrgTree',
  getUnitTreeNew = '/base/hubKm/findSuitUnitOrgAndSuitTree',
  // create = '/base/baseSuitunitOrg/create', //新增绩效部门
  create = '/base/baseRel/updateOrgAndRel',
  asynDeptList = '/base/hubKm/findDeptTree', //异步部门树
  asynUser = '/base/hubKm/findUserTree', //异步人员树
  selectPersonTreeList = '/base/hubKm/getDeptAndPostTree',
  refresh = '/base/baseRel/refresh', //刷新
  createNew = '/base/baseRel/updateOrgAndRelAndSuit',
}

export const deleteOne = (params) => defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true });

export const getDeptTree = (params) => defHttp.get({ url: Api.deptList, params });

export const getParentDept = (params) => defHttp.get({ url: Api.parentDept, params });

export const getUnitTree = (params) => defHttp.get({ url: Api.getUnitTree, params });

export const getUnitTreeNew = (params?) => defHttp.get({ url: Api.getUnitTreeNew, params });

export const createUnit = (params) => {
  return defHttp.post({ url: Api.create, params });
};

export const createUnitNew = (params) => {
  return defHttp.post({ url: Api.createNew, params });
};

export const getAsynDeptTree = (params) => {
  const result = [];
  return new Promise((resolve) => {
    defHttp.get({ url: Api.asynDeptList, params }, { joinParamsToUrl: true }).then((res) => {
      console.log('res', res);
      resolve(res);
      // defHttp.get({ url: Api.parentDept, params: { code: params.id } }).then((parent) => {
      //   console.log('parent', parent);
      //   // parent.children = res;
      //   result.push(parent);
      //   resolve(result);
      // });
    });
  });
};

export const getAsynKeyUser = (params) => {
  return new Promise((resolve) => {
    defHttp.get({ url: Api.asynUser, params }, { joinParamsToUrl: true }).then((res) => {
      res.forEach((r) => {
        if (!r.isUser) {
          r.selectable = false;
        }
      });
      resolve(res);
    });
  });
};

/**
 * @description: Get sample options value
 */
export const orgTypeList = () => defHttp.post({ url: Api.orgType });

/**
 * author:lhw
 * description: 查询上级绩效考核单位
 * @params: @
 * date: 2023/2/13
 * lastModify: 2023/2/13
 */

export const getParentOrg = () => {
  return new Promise((resolve) => {
    defHttp.post({ url: Api.parentOrg }).then((res) => {
      res.push({
        suitUnitName: '无',
        suitUnitCode: -1,
      });
      resolve(res);
    });
  });
};

/**
 * author:lhw
 * description: 查询积分模式默认值
 * @params: @
 * date: 2023/2/13
 * lastModify: 2023/2/13
 */

export const getPointsDefault = (params) => defHttp.post({ url: Api.pointsDefault, params }, { joinParamsToUrl: true });

export const getGroupList = () => defHttp.post({ url: Api.orgGroupList });

export const handleData = (params) => defHttp.post({ url: Api.update, params });

export const createNewUnit = (params) => defHttp.post({ url: Api.createNewUnit, params });

export const selectPersonTreeList = (params?) => {
  const titleMap = (arr = []) => {
    arr.forEach((i) => {
      // i.title = i.orgName;
      i.value = i.id + '';
      // i.id = i.deptId;
      i.key = i.id + '';
      i.disabled = !i.isLeaf;
      i.psnNum = i.desc ? i.desc.psnNum : i.id + '';
      if (i.desc?.psnNum) {
        i.psnNum = i.desc.psnNum;
        // i.key = i.desc.psnNum + '';
      }
      if (i.children?.length) {
        titleMap(i.children);
      }
    });
  };
  return new Promise((resolve, reject) => {
    defHttp.get({ url: Api.selectPersonTreeList, params }).then((res) => {
      titleMap(res);
      console.log('res-------------岗位人员树', res);
      resolve(res);
    });
  });
  // return defHttp.get({ url: Api.selectSuperDepartTreeList, params });
};

export const refresh = (params) => defHttp.post({ url: Api.refresh, params, timeout: 5 * 60 * 1000 });
